package net.orange.architecture.audit;

import java.io.CharArrayWriter;
import java.io.PrintWriter;

import net.orange.architecture.system.SystemParameters;
import net.orange.framework.model.User;

/**
 * The system exception message.
 */
public class ExceptionAuditInfo implements IAuditInfo {

	/**
	 * The exception obejct.
	 */
	private Throwable throwable;

	/**
	 * The current object.
	 */
	private User user;

	/**
	 * The constructor with the exception object and current user.
	 * @param throwable
	 *            The exception object.
	 * @param user
	 *            The user.
	 */
	public ExceptionAuditInfo(Throwable exception, User user) {
		this.throwable = exception;
		this.user = user;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see net.orange.architecture.audit.IAuditInfo#toInfo()
	 */
	public String toInfo() {
		String u = (user != null) ? user.getUser_name() : "visitor";

		if (!SystemParameters.AD_EXCEPTION_LOG_DETAIL) {
			return u + " meet expcetion : " + throwable.getMessage();
		} else {
			CharArrayWriter cw = new CharArrayWriter();
			PrintWriter pw = new PrintWriter(cw, true);
			throwable.printStackTrace(pw);
			return u + " meet expcetion : " + cw.toString();
		}
	}
}
